package entities;

import exceptions.NonexistentEntityException;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author sha
 */
public class CeritaModel {

    public CeritaModel() {
        emf = Persistence.createEntityManagerFactory("CeritaUnyilPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public List<Cerita> getCerita(long idpropinsi) {
        List<Cerita> cerita = new ArrayList<Cerita>();

        EntityManager em = getEntityManager();
        try {
            //long longvar = 2;
            Query q = em.createQuery("SELECT a FROM Cerita AS a LEFT JOIN a.propinsi p WHERE p.id=" + idpropinsi);
            //q.setParameter("propinsi", longvar);
            //q.setParameter("prop", longvar);
            cerita = q.getResultList();

        } finally {
            em.close();
        }
        return cerita;
    }

    public Cerita findCerita(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Cerita.class, id);
        } finally {
            em.close();
        }
    }

    public void rekamCerita(Cerita cerita) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(cerita);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void updateCerita(Cerita cerita) {
        EntityManager em = getEntityManager();
        try {
            em.getTransaction().begin();
            em.merge(cerita);
            em.getTransaction().commit();
        } finally {
            em.close();
        }
    }

    public void deleteCerita(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Cerita cerita;
            try {
                cerita = em.getReference(Cerita.class, id);
                cerita.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The user with id " + id + " no longer exists.", enfe);
            }
            em.remove(cerita);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public Cerita findCerita(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Cerita.class, id);
        } finally {
            em.close();
        }
    }

    public void edit(Cerita cerita) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            cerita = em.merge(cerita);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(long idcerita) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Cerita cerita;
            try {
                cerita = em.getReference(Cerita.class, idcerita);
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("user belum dipilih.", enfe);
            }
            em.remove(cerita);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }
}